<template>
  <div class="app-container">
    <el-form class="base-search-form" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="巡更任务" prop="taskName">
        <el-input
            v-model="queryParams.taskName"
            placeholder="请输入巡更任务名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="巡更人" prop="userId">
        <el-select v-model="queryParams.userId" placeholder="请选择" clearable>
          <el-option
            v-for="item in patrolUser"
            :key="item.user_id"
            :label="item.user_name"
            :value="item.user_id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="巡更任务时间">
        <el-date-picker clearable
          v-model="searchDate"
          type="daterange"
          value-format="YYYY-MM-DD"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          :shortcuts="shortcuts"
          @change="confirmSearchDate">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="base-container mb-50px">
      <el-row :gutter="10" class="mb-10px">
        <el-col :span="1.5">
          <el-button
              type="warning"
              plain
              icon="Download"
              @click="handleExport"
              v-hasPermi="['plat:patrolRecord:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table border v-loading="loading" :data="recordList">
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column label="任务名称" align="center" prop="taskName" />
        <el-table-column label="巡更人" align="center" prop="userName" />
        <el-table-column label="任务执行开始时间" align="center" prop="startTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="任务执行结束时间" align="center" prop="endTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="执行情况" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="patrol_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="巡更结果" align="center" prop="result">
          <template #default="scope">
            <dict-tag :options="patrol_result" :value="scope.row.result" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="80" align="center" fixed="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="详情" placement="top">
              <el-button link type="primary" icon="View" @click="showDetail(scope.row)" v-hasPermi="['plat:patrolRecord:query']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Record">
import { listPatrolRecord  } from "@/api/patrol/record"
import { getSelectData } from "@/api/patrol/task"
import { nextTick } from "vue"
import { useRouter } from "vue-router"
import RecordDetail from './detail.vue'

const recordDetail = ref()
const router = useRouter()
const { proxy } = getCurrentInstance()
const { patrol_status, patrol_result } = proxy.useDict("patrol_status", "patrol_result")

const recordList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)
const patrolUser = ref([])
const searchDate = ref([])

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    taskName: null,
    userId: null,
    startTime: null,
    endTime: null,
  },
})
const shortcuts = [
  {
    text: '今日',
    value: [new Date(), new Date()],
  },
  {
    text: '昨日',
    value: () => {
      const date = new Date()
      const lastDay = date.setTime(date.getTime() - 3600 * 1000 * 24)
      return [lastDay, lastDay]
    },
  },
  {
    text: '近七日',
    value: () => {
      const date = new Date()
      const date1 = new Date()
      const last7Day = date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
      const lastDay = date1.setTime(date1.getTime() - 3600 * 1000 * 24)
      return [last7Day, lastDay]
    },
  },
]

const { queryParams } = toRefs(data)

const confirmSearchDate = (e) => {
  if (e && e.length) {
    queryParams.value.startTime = e[0]
    queryParams.value.endTime = e[1]
  } else {
    queryParams.value.startTime = null
    queryParams.value.endTime = null
  }
}

/** 获取巡更人列表 */
function getUserList() {
  getSelectData().then(response => {
    patrolUser.value = response.data.patrolUser
  })
}

/** 查询巡更记录列表 */
function getList() {
  loading.value = true
  listPatrolRecord(queryParams.value).then(response => {
    recordList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  searchDate.value = []
  queryParams.value.startTime = null
  queryParams.value.endTime = null
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 详情 */
function showDetail(row) {
  router.push(`/patrol/recorddetail/${row.id}`)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('plat/patrolRecord/export', {
    ...queryParams.value
  }, `巡更记录_${proxy.getFileImportName()}.xlsx`)
}

getUserList()
getList()
</script>
